package com.anolesoft.amljmes.billing.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.anolesoft.amljmes.billing.dto.AmBillingCondition;
import com.anolesoft.amljmes.billing.dto.AmBillingInfoDTO;
import com.anolesoft.amljmes.billing.dto.AmBillingInfoExportDTO;
import com.anolesoft.amljmes.billing.service.AmBillingInfoService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.demo.PageResult;
//import com.anolesoft.yr.util.Constants;
//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.LocalDateTimeConverter;
import com.anolesoft.yr.util.PageData;

import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.security.util.SecurityUtils;

@RestController
@CrossOrigin
@RequestMapping(value="/amBillingInfo")
@Slf4j
public class AmBillingInfoController {
	
	@Autowired
	AmBillingInfoService amBillingInfoService;
	
	/**
	 * 上下总面板查询
	 * @param amBillingCondition
	 * @return
	 */
	@PostMapping(value="/query", produces = MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult queryAmBillingInfo(@RequestBody AmBillingCondition amBillingCondition) {
		log.debug("amBillingCondition:{}",amBillingCondition);
		PageData<List<AmBillingInfoDTO>> pageInfo = amBillingInfoService.queryAmBillingInfo(amBillingCondition);
		System.out.println(">>>amBillingCondition:"+pageInfo.getTotal());
		PageResult pageResult = new PageResult(pageInfo.getItems(),amBillingCondition.getPageSize(), amBillingCondition.getPageNo(), pageInfo.getTotal());
		System.out.println(">>:"+amBillingCondition.getPageSize());
		System.out.println(">>:"+amBillingCondition.getPageNo());
		System.out.println(">>:"+pageInfo.getItems().size());
        AjaxResult success = AjaxResult.success();
        success.put("result", pageResult);
		return success;
	}
	@PostMapping(value="/query-nopage", produces = MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult queryAllByCondition(@RequestBody AmBillingCondition amBillingCondition) {
		log.debug("amBillingCondition:{}",amBillingCondition);
		LoginUser loginUser = SecurityUtils.getLoginUser();
		amBillingCondition.setFactoryId(loginUser.getFacCode());
		List<AmBillingInfoDTO> amBillingInfoDTOS = amBillingInfoService.queryAllByConditon(amBillingCondition);
		System.out.println(amBillingCondition.toString()+"__________________________________");
		int pn =  amBillingCondition.getPageNo().intValue();
		int ps = amBillingCondition.getPageSize().intValue();
		int endIndex = pn * ps > amBillingInfoDTOS.size() ? amBillingInfoDTOS.size() : pn*ps;
		List<AmBillingInfoDTO> list = amBillingInfoDTOS.subList( (pn-1) *ps, endIndex  );
		PageResult pageResult = new PageResult(list, amBillingCondition.getPageSize(), amBillingCondition.getPageNo(), amBillingInfoDTOS.size());
		return AjaxResult.success("查询成功",pageResult);
	}
	/**
	 * 导出查询
	 * @param amBillingCondition
	 * @return
	 */
	@PostMapping(value="/export", produces = MediaType.APPLICATION_JSON_VALUE)
	public void queryAmBillingInfoToExport(@RequestBody AmBillingCondition amBillingCondition,HttpServletResponse response) {
		Integer pageNum = 1;
		Integer pageSize = 5000;
		amBillingCondition.setPageNo(pageNum);
		amBillingCondition.setPageSize(pageSize);
		PageData<List<AmBillingInfoDTO>> pageInfo = amBillingInfoService.queryAmBillingInfo(amBillingCondition);
		OutputStream ops = null;
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		
		try {
			response.setHeader("Content-Disposition", "attachment;filename="+new String(("开单信息.xlsx").getBytes("gb2312"),"ISO8859-1"));
	
			ops = response.getOutputStream();

			EasyExcel.write(ops,AmBillingInfoExportDTO.class).registerConverter(new LocalDateTimeConverter()).sheet("开单信息").doWrite(pageInfo.getItems());
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(ops!=null) {
				try {
					ops.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		//return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE);
	}
	
	@GetMapping(value="/queryReceiveByCode",produces= MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult queryReceiveAmBillingInfo(@RequestBody AmBillingCondition amBillingCondition) {
		try {
			AmBillingInfoDTO data = amBillingInfoService.queryReceiveAmBillingInfo(amBillingCondition);
			return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, data);
		}catch(RuntimeException e){
			return new AjaxResult(HttpStatus.ERROR, e.getMessage());
		}
	}
	/**
	 * 开单界面-通过制造记录条码查询
	 * @param amBillingCondition
	 * @return
	 */
	@PostMapping(value="/queryByCode",produces= MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult queryMesOrderTraceInfo(@RequestBody AmBillingCondition amBillingCondition) {
		AmBillingInfoDTO amBillingInfoDTO = amBillingInfoService.queryMesOrderTraceInfo(amBillingCondition);
		if(amBillingCondition.getHandoverCategory()!=null) {
			amBillingInfoDTO.setHandoverCategory(amBillingCondition.getHandoverCategory());
		}
		if(amBillingCondition.getWorkshopName()!=null) {
			amBillingInfoDTO.setWorkshopName(amBillingCondition.getWorkshopName());	
		}		
		return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,amBillingInfoDTO);
	}
	
	/**
	 * 开单界面-确认开单按钮功能-将开单界面信息插入开单信息表
	 * @param amBillingInfoDTOs
	 * @return
	 */
	@PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult insertAmBillingInfo(@RequestBody List<AmBillingInfoDTO> amBillingInfoDTOs){
		System.out.println(amBillingInfoDTOs);
		return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE,amBillingInfoService.insertAmBillingInfo(amBillingInfoDTOs));
	}
	/**
	 * 开单撤回
	 * @param amBillingInfoDTOs
	 * @return
	 */
	@DeleteMapping(value="/delete", produces = MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult deleteAmBillingInfoRecall(@RequestBody List<AmBillingInfoDTO> amBillingInfoDTOs) {
//		return new AjaxResult(HttpStatus.SUCCESS,Constants.DELETE_SUCCESS_MESSAGE,amBillingInfoService.deleteAmBillingInfoRecall(amBillingInfoDTOs));
		return new AjaxResult(HttpStatus.SUCCESS,Constants.DELETE_SUCCESS_MESSAGE,amBillingInfoService.revertAmbillingInfoBatch(amBillingInfoDTOs));
	}
	
	/**
	 * 开单撤回-单行
	 * @param objuid
	 * @return
	 */
	@DeleteMapping(value="/delete/single", produces = MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult deleteAmBillingInfoRecallSingle(@RequestBody String objuid) {
//		return new AjaxResult(HttpStatus.SUCCESS,Constants.DELETE_SUCCESS_MESSAGE,amBillingInfoService.deleteAmBillingInfoRecallSingle(objuid));
		return new AjaxResult(HttpStatus.SUCCESS,Constants.DELETE_SUCCESS_MESSAGE,amBillingInfoService.revertAmbillingInfo(AmBillingInfoDTO.builder().objuid(objuid).build()));
	}
	/**
	 * 接单
	 * @param amBillingInfoDTOs
	 * @return
	 */
	@PutMapping(value = "/update/receive", produces = MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult updateAmbillingInfoReceive(@RequestBody List<AmBillingInfoDTO> amBillingInfoDTOs) {
		return new AjaxResult(HttpStatus.SUCCESS,Constants.MODIFY_SUCCESS_MESSAGE,amBillingInfoService.updateAmbillingInfoReceive(amBillingInfoDTOs));
	}
	/**
	 * 接单-拒绝-填写拒绝原因
	 * @param amBillingInfoDTOs
	 * @return
	 */
	@PutMapping(value = "/update/refuse", produces = MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult updateAmbillingInfoRefuse(@RequestBody List<AmBillingInfoDTO> amBillingInfoDTOs) {
		return new AjaxResult(HttpStatus.SUCCESS,Constants.MODIFY_SUCCESS_MESSAGE,amBillingInfoService.updateAmbillingInfoRefuse(amBillingInfoDTOs));
	}
	/**
	 * 接单界面-通过制造记录条码查询
	 * @param amBillingCondition
	 * @return
	 */
	@PostMapping(value="/queryByCode/receive",produces= MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult queryBillingInfoByPacrNoReceive(@RequestBody AmBillingCondition amBillingCondition) {
		AmBillingInfoDTO amBillingInfoDTO = amBillingInfoService.queryBillingInfoByPacrNo(amBillingCondition.getPacrNo());
		return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,amBillingInfoDTO);
	}
	@PostMapping(value="/queryByCode/receive2",produces= MediaType.APPLICATION_JSON_VALUE)
	public AjaxResult queryBillingInfoByPacrNoReceive2(@RequestParam("pacrNo") String pacrNo) {
		AmBillingInfoDTO amBillingInfoDTO = amBillingInfoService.queryBillingInfoByPacrNo(pacrNo);
		return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,amBillingInfoDTO);
	}
}
